package labox.innovation.gameserver.script.faenor;

import java.util.Date;

import javax.script.ScriptContext;

import labox.innovation.config.Config;
import labox.innovation.gameserver.ThreadPoolManager;
import labox.innovation.gameserver.script.DateRange;
import labox.innovation.gameserver.script.IntList;
import labox.innovation.gameserver.script.Parser;
import labox.innovation.gameserver.script.ParserFactory;
import labox.innovation.gameserver.script.ScriptEngine;
import labox.innovation.gameserver.timer.Timer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Node;

/**
 * @author Luis Arias
 * 
 */
public class FaenorEventParser extends FaenorParser {
	static Logger _log = LoggerFactory.getLogger(FaenorEventParser.class.getName());
	private DateRange _eventDates = null;

	@Override
	public void parseScript(final Node eventNode, ScriptContext context) {
		String ID = attribute(eventNode, "ID");

		if (_log.isDebugEnabled())
			_log.debug("Parsing Event \"{" + ID + "}\"" , Config.SERVER_ID, "�ű�����");

		_eventDates = DateRange.parse(attribute(eventNode, "Active"), DATE_FORMAT);

		Date currentDate = new Date();
		if (_eventDates.getEndDate().before(currentDate)) {
			_log.info("Event ID: ( {" + ID + "} ) has passed... Ignored.", Config.SERVER_ID, "�ű�����");
			return;
		}

		if (_eventDates.getStartDate().after(currentDate)) {
			_log.info("Event ID: (" + ID + ") is not active yet... Ignored.", Config.SERVER_ID, "�ű�����");
			int t = (int) (_eventDates.getStartDate().getTime() - currentDate.getTime());
			new Timer(t, false, new Runnable() {
				public void run() {
					parseEventDropAndMessage(eventNode);
				}
			}).start();

			return;
		}

		parseEventDropAndMessage(eventNode);
	}

	protected void parseEventDropAndMessage(Node eventNode) {

		for (Node node = eventNode.getFirstChild(); node != null; node = node.getNextSibling()) {

			if (isNodeName(node, "DropList")) {
				parseEventDropList(node);
			} else if (isNodeName(node, "Message")) {
				parseEventMessage(node);
			}
		}
	}

	private void parseEventMessage(Node sysMsg) {
		if (_log.isDebugEnabled())
			_log.debug("Parsing Event Message.", Config.SERVER_ID, "�ű�����");

		try {
			String type = attribute(sysMsg, "Type");
			String[] message = attribute(sysMsg, "Msg").split("\n");

			if (type.equalsIgnoreCase("OnJoin")) {
				_bridge.onPlayerLogin(message, _eventDates);
			}
		} catch (Exception e) {
			_log.error("Error in event parser.", Config.SERVER_ID, "�ű�����");
			e.printStackTrace();
		}
	}

	private void parseEventDropList(Node dropList) {
		if (_log.isDebugEnabled())
			_log.debug("Parsing Droplist.", Config.SERVER_ID, "�ű�����");

		for (Node node = dropList.getFirstChild(); node != null; node = node.getNextSibling()) {
			if (isNodeName(node, "AllDrop")) {
				parseEventDrop(node);
			}
		}
	}

	private void parseEventDrop(Node drop) {
		if (_log.isDebugEnabled())
			_log.debug("Parsing Drop.", Config.SERVER_ID, "�ű�����");

		try {
			int[] items = IntList.parse(attribute(drop, "Items"));
			int[] count = IntList.parse(attribute(drop, "Count"));
			double chance = getPercent(attribute(drop, "Chance"));

			_bridge.addEventDrop(items, count, chance, _eventDates);
		} catch (Exception e) {
			_log.error("ERROR(parseEventDrop)", Config.SERVER_ID, e);
		}
	}

	static class FaenorEventParserFactory extends ParserFactory {
		@Override
		public Parser create() {
			return (new FaenorEventParser());
		}
	}

	static {
		ScriptEngine.parserFactories.put(getParserName("Event"), new FaenorEventParserFactory());
	}
}
